<?php
/**
 * Created by PhpStorm.
 * User: YONBIN
 * Date: 2020/12/3
 * Time: 9:33
 * Email: 479703456@qq.com
 */

namespace app\domain\admin;


use app\model\AdminMenus;

class AdminMenusDomain extends CommonDomain
{
    public function retrieve(): array
    {
        try {
            $model = app(AdminMenus::class);
            $total = $model->count();
            $list = $model->order('sort ASC,id DESC')->select();
            $list->filter(function (&$data) {
                if ($data->pid) {
                    $parent = app(AdminMenus::class)->find($data->pid);
                    $data['parent_title'] = $parent->title ?? '';
                } else {
                    $data['parent_title'] = '';
                }
            });

            $list = commonRecursion($list, 0, 0, 'pid', 'children');

            return [
                'total' => $total,
                'list' => $list
            ];
        } catch (\Exception $e) {
            $this->errMsg = $e->getMessage();
            return [];
        }
    }

    public function create($pid, $title, $icon, $route, $redirect, $component_name, $component, $sort, $status): bool
    {
        try {
            $result = app(AdminMenus::class)->save([
                'pid' => $pid,
                'title' => $title,
                'icon' => $icon,
                'route' => $route,
                'redirect' => $redirect,
                'component_name' => $component_name,
                'component' => $component,
                'sort' => $sort,
                'status' => $status,
            ]);

            return (bool)$result;
        } catch (\Exception $e) {
            $this->errMsg = $e->getMessage();
            return false;
        }
    }

    public function update($id, $pid, $title, $icon, $route, $redirect, $component_name, $component, $sort, $status): bool
    {
        try {
            $info = app(AdminMenus::class)->find($id);
            if (empty($info)) {
                $this->errMsg = 'record.not_exists';
                return false;
            }

            $result = $info->save([
                'pid' => $pid,
                'title' => $title,
                'icon' => $icon,
                'route' => $route,
                'redirect' => $redirect,
                'component_name' => $component_name,
                'component' => $component,
                'sort' => $sort,
                'status' => $status,
            ]);

            return (bool)$result;
        } catch (\Exception $e) {
            $this->errMsg = $e->getMessage();
            return false;
        }
    }

    public function delete($id): bool
    {
        try {
            $info = app(AdminMenus::class)->with('relation')->find($id);
            if (empty($info)) {
                $this->errMsg = 'record.not_exists';
                return false;
            }

            $result = $info->together(['relation'])->delete();
            app(AdminMenus::class)->setOption('soft_delete', ['delete_time', time()])->where(['pid' => $id])->delete();

            return (bool)$result;
        } catch (\Exception $e) {
            $this->errMsg = $e->getMessage();
            return false;
        }
    }
}